<?php

class DB {
    private $pdo = null;
    private $dsn ='mysql:host=localhost;dbname=my_user;charset=utf8';
    private  $dbname = "my_user";
    private $username = 'root';
    private $userpsw = '123456';
//    错误提示
    private $errors ="PDO::ERRMODE_EXCEPTION";
//    返回语句类型
    private $fetch_type = "PDO::FETCH_ASSOC";
//    初始化参数
    public function __construct($config=[]) {
        if(isset($config)){
            if(!is_array($config)){
                return '参数错误'."<hr>";
            }
            if(isset($config['dbname'])&& is_string($config['dbname'])){
                $this->dsn = 'mysql:host=localhost;dbname='.$config['dbname'].';charset=utf8';
            }
            if(isset($config['username'])&& is_string($config['username'])){
                $this->username = $config['username'];
            }
            if(isset($config['userpsw'])&& is_string($config['userpsw'])){
                $this->userpsw = $config['userpsw'];
            } 
            if(isset($config['errors'])&& is_string($config['errors'])){
                $this->errors = $config['errors'];
            } 
        }
        $this->ConnectDB();
    }

//    连接数据库
    public function  ConnectDB(){
       try{
           $this->pdo = new PDO($this->dsn, $this->username, $this->psw, array(
                PDO::ATTR_ERRMODE=>  $this->errors
           ));
       } catch (Exception $ex) {
           return "数据库连接失败".'<hr>';
       }
    }
//    关闭数据库
    public function CloseDB(){
        $this->pdo =null;
    }
//    操作数据库
    public function QueryDB($sql ,$parm =null,$fetch_type = PDO::FETCH_ASSOC){
        $stmt = $this->pdo->prepare($sql);
        if(isset($parm)&&  is_array($parm)){
            if (array_values($parm)==$parm){
                for($i=0;$i<count($parm);$i++){
                    $stmt->bandParam($i+1,$parm[$i]);
                }  
            }else{
                foreach ($param as $key => $value) {
                    $array[] = "$stmt->bindParam($key, $value)";
                } 
                for($i=0;$i<count($array);$i++){
                    $array[$i];
                }  
            }
        }
        
        $stmt->execute();
        $sql = trim($sql);
        $sql_type = explode(' ', $sql)[0];
        $sql_type = strtolower($sql_type);
        if ($sql_type == 'insert' || 
                    $sql_type == 'update' ||
                    $sql_type == 'delete'||
                    $sql_type == 'create') {
                return $stmt->rowCount();
            } else if ($sql_type == 'select' || 
                    $sql_type == 'show') {
                return $stmt->fetchAll($fetch_type);
            } 
       
    }
//    返回上一个操作行的ID
    public function LastID(){
        return $this->pdo->lastInsertId();
    }
//    事务
//    开启事务
    public function beginTransaction(){
        $this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
        $this->pdo->beginTransaction();
    }
//    提交事务
    public function commitTransaction(){
        $this->pdo->commit();
        $this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
    }
    // 回滚 
    public function rollBackTransaction() {
        $this->pdo->rollback();
        $this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
    }
    
    
    
    
    
}
